<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Http\Controllers\OA\CommonController;
use App\Model\OACompany;
use App\Model\OAMatter;
use App\Model\OAOrders;
use App\Model\OAProjectLx;
use App\Model\OASupplier;
use Illuminate\Http\Request;

class OrderLogic extends Controller
{
    public function getOrderDetail($params)
    {
        $order = OAOrders::query()->where('id',$params['id'])->first();
        $is_company = $params['is_company'];
        if($is_company){
            $list = OAMatter::query()->where('order_id',$params['id'])
                ->groupBy('supplier_id')
                ->orderBy('money','ASC')
                ->limit(20)
                ->select()
                ->get()
                ->toArray();
        }else{
            $list = OAMatter::query()->where('order_id',$params['id'])
                ->where('supplier_id',$params['uid'])
                ->orderBy('add_time','ASC')
                ->limit(20)
                ->select()
                ->get()
                ->toArray();
        }

        //dump($list);
        $project = OAProjectLx::query()->where('id',$order->xmid)->first();
        $order->batch_id = $project->batch_id;
        $order->name = $project->name;
        $order->company = OACompany::query()->where('id',$project->gsid)->value('name');
        $supplierList = OASupplier::query()->pluck('company','id')->toArray();
        //dump($list);
        $moneyList = array_column($list,'money');
        //dd($moneyList);
        sort($moneyList);
        $chart = ['一','二','三','四','五','六','七','八','九','十','十二','十三','十四','十五','十六','十七','十八','十九','二十',];
        foreach ($list as &$item){
        $item['limit_money'] = $order->limit_money;
        $item['reduce_money'] = $order->reduce_money;
        $pos = array_search($item['money'],$moneyList);
        $item['rank_id'] = $chart[$pos];
        $item['supplier'] = $supplierList[$item['supplier_id']];
        }
        //dd($list);
        $order->list = $list;
        $order->files = json_decode($order->files,true);
        return $order;

    }

    //获取订单列表
    public function getOrderList($params)
    {

        $isCompnay = $params['is_company'];
        $orderIds = [];
        if(!$isCompnay){
            $supplierID = $params['uid'];
            $orderIds = OAMatter::query()->where('supplier_id',$supplierID)->pluck('order_id')->toArray();
        }
        if(isset($params['status'])) {
            $orderIds = [];
        }
        $query = OAOrders::query();
        if($orderIds) $query->whereIn('id',$orderIds);
        $list = $query->select(['id','xmid','status'])->get()->toArray();

        $ret = ['processing'=>[],'completed'=>[],'waiting'=>[]];
        $common = (new CommonLogic())->getCommonData(['company']);
        $company = array_column($common['company'],'name','id');
        foreach ($list as $item){
            $project = OAProjectLx::query()->where('id',$item['xmid'])->first(['name','batch_id','gsid']);
            $project = $project->toArray();
            if($item['status'] == 1){
                $ret['processing'][] = [
                    'id'=>$item['id'],
                    'title'=>$project['name'],
                ];
            }elseif($item['status'] == 2){
                $ret['completed'][] = [
                    'id'=>$item['id'],
                    'title'=>$project['name'],
                ];
            }else{
                $ret['waiting'][] = [
                    'id'=>$item['id'],
                    'name'=>$project['name'],
                    'xmid'=>$item['xmid'],
                    'batch_id'=>$project['batch_id'],
                    'company'=>$company[$project['gsid']] ?? '-'
                ];
            }

        }
        return $ret;
    }

    //获取订单
    public function getOrderByXmid($params)
    {
        return OAOrders::query()->where('xmid',$params['xmid'])->first();
    }

}
